#!/bin/sh
#################################################################################
# /app/app/tengine目录下
#
#################################################################################
cur_dir=$(cd `dirname $0`; pwd)
cd $cur_dir
echo "cur_dir:$cur_dir"
docker_image_version=3.2.0
if [[ -n $1 ]];then
  docker_image_version=$1
fi
docker_image_flag=capp_nginx_${docker_image_version}
echo "docker image:$docker_image_flag"
app_ip=16.77.77.102
app_port=7070
app_api=http://${app_ip}:${app_port}
cd $cur_dir
##生成Dockerfile文件
cat << EOF > nginx_docker.conf
worker_processes auto;
worker_rlimit_nofile 65535;
events {
    use epoll;
    multi_accept on;
    worker_connections 1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                  '\$status \$body_bytes_sent "\$http_referer" '
                  '"\$http_user_agent" "\$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log main;
    error_log   /var/log/nginx/error.log warn;

## 基本设置
#pagespeed on;
#pagespeed FileCachePath /var/ngx_pagespeed_cache;
## 禁用CoreFilters
#pagespeed RewriteLevel PassThrough;
## 启用必要的优化过滤器
#pagespeed EnableFilters collapse_whitespace;
#pagespeed EnableFilters canonicalize_javascript_libraries;
#pagespeed EnableFilters combine_css;
##pagespeed EnableFilters combine_javascript;
#pagespeed EnableFilters elide_attributes;
#pagespeed EnableFilters extend_cache;
#pagespeed EnableFilters flatten_css_imports;
#pagespeed CssFlattenMaxBytes 5120;
#pagespeed EnableFilters lazyload_images;
##pagespeed EnableFilters rewrite_javascript;
#pagespeed EnableFilters insert_dns_prefetch;
#pagespeed EnableFilters prioritize_critical_css;
## 禁止对特定目录的优化（可选）
#pagespeed Disallow "*/system/*";
## 图片处理配置
#pagespeed EnableFilters lazyload_images;
## 不加载显示区域以外的图片
#pagespeed LazyloadImagesAfterOnload off;
##pagespeed LazyloadImagesBlankUrl "https://www.xxx.com/xxx.png";
## 启用图片优化机制(主要是 inline_images, recompress_images, convert_to_webp_lossless（这个命令会把PNG和静态Gif图片转化为webp）, and resize_images.)
#pagespeed EnableFilters rewrite_images;
##组合 convert_gif_to_png, convert_jpeg_to_progressive, convert_jpeg_to_webp, convert_png_to_jpeg, jpeg_subsampling, recompress_jpeg, recompress_png, recompress_webp, #strip_image_color_profile, and strip_image_meta_data.
#pagespeed EnableFilters recompress_images;
## 将JPEG图片转化为webp格式
#pagespeed EnableFilters convert_jpeg_to_webp;
## 将动画Gif图片转化为动画webp格式
#pagespeed EnableFilters convert_to_webp_animated;
## 图片预加载
#pagespeed EnableFilters inline_preview_images;
## 让JS里引用的图片也加入优化
#pagespeed InPlaceResourceOptimization on;
#pagespeed EnableFilters in_place_optimize_for_browser;
    proxy_buffering on;
    proxy_buffer_size 8k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64K;
    proxy_temp_path /var/nginxtemp 1 2;
    proxy_max_temp_file_size 20m;
    proxy_temp_file_write_size 256k;
    #cache
    proxy_cache_path /var/nginxcache levels=1:1 keys_zone=cache_one:200m inactive=3d max_size=30g;
    server_tokens off;
    keepalive_timeout 60;
    keepalive_requests 1024;
    client_header_buffer_size 512k;
    client_body_buffer_size 1m;
    client_max_body_size 256m;
    sendfile on;
    #big file
    tcp_nopush on;
    #small file
    tcp_nodelay on;
    open_file_cache max=65535 inactive=60s;
    open_file_cache_valid 60s;
    open_file_cache_min_uses 1;
    open_file_cache_errors on;
    gzip on;
    gzip_static on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_buffers 16 8k;
    gzip_types image/jpeg image/gif image/x-icon image/png application/font-woff2 text/plain text/css application/x-font-ttf application/json application/x-javascript application/javascript text/javascript;
    gzip_vary on;
    gzip_proxied any;
    gzip_disable "MSIE [1-6]\\.";
    gzip_http_version 1.1;
    add_header Access-Control-Allow-Origin *;
    server {
        listen *:8899 default_server;
        server_name resource.xyjm.store;
        location /ngx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
          }
    }
    server {
        listen 7000;
        server_name resource.xyjm.store;
        keepalive_timeout 60;
        location / {
          proxy_pass $app_api;
          proxy_redirect off;
          proxy_set_header Host \$host:\$server_port;
          proxy_set_header X-Real-IP \$remote_addr;
          proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        }
        location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
          access_log off;
          add_header Cache-Control public;
          proxy_pass $app_api;
          proxy_redirect off;
          proxy_set_header Host \$host:\$server_port;
          proxy_set_header X-Real-IP \$remote_addr;
          proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
          add_header X-Cache-Status \$upstream_cache_status;
          proxy_cache_key \$host\$uri\$is_args\$args;
          proxy_cache cache_one;
          proxy_cache_valid 200 7d;
          proxy_cache_valid 302 1h;
          proxy_cache_valid 301 1d;
          proxy_cache_valid any 1m;
          expires 30d;
        }
        location /business/common/page_info/ {
          proxy_pass $app_api;
          proxy_redirect off;
          add_header Cache-Control public;
          proxy_set_header Host \$host:\$server_port;
          proxy_set_header X-Real-IP \$remote_addr;
          proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
          add_header X-Cache-Status \$upstream_cache_status;
          proxy_cache_key \$host\$uri\$is_args\$args;
          proxy_cache cache_one;
          proxy_cache_valid 200 7d;
          proxy_cache_valid 302 1h;
          proxy_cache_valid 301 1d;
          proxy_cache_valid any 1m;
          expires 30d;
         }
      location /assets/ {
          proxy_pass $app_api;
          proxy_redirect off;
          proxy_set_header Host \$host:\$server_port;
          proxy_set_header X-Real-IP \$remote_addr;
          proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
          add_header X-Cache-Status \$upstream_cache_status;
          proxy_cache_key \$host\$uri\$is_args\$args;
          proxy_cache cache_one;
          proxy_cache_valid 200 7d;
          proxy_cache_valid 302 1h;
          proxy_cache_valid 301 1d;
          proxy_cache_valid any 1m;
          expires 30d;
         }
    }
}
EOF
cd $cur_dir
cat << EOF > Dockerfile
FROM registry.cn-hangzhou.aliyuncs.com/lank/nginx:s_1.21.5
MAINTAINER NGINX
RUN mkdir -p              /nginx/cert
RUN mkdir -p              /nginx/html
RUN mkdir -p              /var/log/nginx
COPY nginx_docker.conf    /etc/nginx/nginx.conf
EOF
##build 生成image
docker build ./ -t registry.cn-hangzhou.aliyuncs.com/lank/nginx:$docker_image_flag
exit 0



